﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using BSCMFF.Core.DataAccessLayer;
using System.Collections.Specialized;

namespace BSCMFF.Core.RoleDataProvider
{
    public class EntityRoleDataProvider : RoleProvider
    {
        public override void Initialize(string name, NameValueCollection config)
        {

            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (String.IsNullOrEmpty(name))
            {
                name = "EntityRoleDataProvider";
            }

            //if (String.IsNullOrEmpty(config["description"]))
            //{
            //    config.Remove("description");
            //    config.Add("description", "Generic Database Provider");
            //}

            base.Initialize(name, config);
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            //BSCEntities context = new BSCEntities();

            //foreach (string user in usernames)
            //{
            //    var users = (from u in context.Users
            //                 where u.Login == user
            //                 select u).SingleOrDefault();

            //    if (users != null)
            //    {
            //        foreach (string role in roleNames)
            //        {
            //            var roles = (from r in context.Roles
            //                         where r.Name == role
            //                         select r).SingleOrDefault();

            //            if (roles != null)
            //            {

            //            }
            //        }
            //    } 
            //}
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            BSCEntities context = new BSCEntities();

            var roles = from r in context.Roles
                        select r;
            int count = roles.Count();
            string[] roleNames = new string[count];
            //List<string> roleNames = new List<string>();
            int i = 0;
            foreach (Role r in roles)
            {
                //roleNames.Add(r.Name);   
                roleNames[i++] = r.Name;
            }

            //string[] stringNames = new string[roleNames.Count];
            
            return roleNames;
        }

        public override string[] GetRolesForUser(string username)
        {
            BSCEntities context = new BSCEntities();

            string[] roleName = {(from u in context.Users.Include("Role")
                      where u.Login == username
                      select u.Role.Name).FirstOrDefault()};
            return roleName;            
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            BSCEntities context = new BSCEntities();

            User user = (from u in context.Users.Include("Role")
                           where u.Login == username && u.Role.Name == roleName
                           select u).FirstOrDefault();
            return user == null ? false : true;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            BSCEntities context = new BSCEntities();

            Role role = (from r in context.Roles
                         where r.Name == roleName
                         select r).FirstOrDefault();
            return role == null ? false : true;
        }
    }
}
